﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using KBWebApi.Common;
using KBWebApi.Model;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace KBWebApi.Controllers
{
    /// <summary>
    /// 控制用户登录
    /// </summary>
    [Route("api/[controller]")]
    [ApiController]
    public class UserController : ControllerBase
    {
        Bll.UserBll userBll = new Bll.UserBll();
        /// <summary>
        /// 用户登录
        /// </summary>
        /// <param name="userDto">登录实体</param>
        /// <returns></returns>
        [HttpPost]
        [Route("SignIn")]
        public async Task<ActionResult> SignIn(UserDto userDto)
        {
            var result = await userBll.SignIn(userDto);
            if (result != null)
            {
                return result;
            }
            else
            {
                return NotFound();
            }
        }

        /// <summary>
        /// 获取用户信息
        /// </summary>
        /// <param name="token"></param>
        /// <returns></returns>
        [HttpGet]
        [Route("GetUserInfo")]
        public async Task<ActionResult> GetInfo(string token)
        {
            var result = await userBll.GetInfo(token);
            if (result != null)
            {
                return result;
            }
            else
            {
                return NotFound();
            }
        }

        /// <summary>
        /// 获取用户列表(分页查询)
        /// </summary>
        /// <param name="UserId"></param>
        /// <param name="UserName"></param>
        /// <returns></returns>
        [HttpGet]
        [Route("GetList")]
        public async Task<ActionResult> GetList(string UserId, string UserName)
        {
            var result = await userBll.GetList(UserId, UserName);
            return new JsonResult(
                new
                {
                    code = 20000,
                    message = result
                }

            );
        }

        /// <summary>
        /// 新增或修改用户数据
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        [HttpPost]
        //[WabApiAuthorize]
        [Route("NewOrUpdate")]
        public async Task<ActionResult> NewOrUpdate(UserDto user)
        {
            if (user == null)
            {
                return new JsonResult(new
                {
                    code = 20001,
                    message = "失败"
                });
            }

            var result = await userBll.NewOrUpdate(user);
            return result;
        }

        /// <summary>
        /// 修改用户自身名称密码
        /// </summary>
        /// <param name="dic"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("UpdateUserInfo")]
        public async Task<ActionResult> UpdateUserInfo(dynamic dic)
        {
            if (dic == null)
            {
                return new JsonResult(new
                {
                    code = 20001,
                    message = "失败"
                });
            }
            var result = await userBll.UpdateUserInfo(dic);
            return result;
        }

        /// <summary>
        /// 根据ID删除用户数据
        /// </summary>
        /// <param name="UserId"></param>
        /// <returns></returns>
        [HttpGet]
        //[WabApiAuthorize]
        [Route("Delete")]
        public async Task<ActionResult> Delete(string UserId)
        {
            if (string.IsNullOrEmpty(UserId))
            {
                return new JsonResult(new
                {
                    code = 20001,
                    message = "失败"
                });
            }

            var result = await userBll.Delete(UserId);
            return result;
        }
    }
}